//
//  YLMessageBaseViewController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/10/18.
//  Copyright © 2017年 she1323. All rights reserved.
//  通知 公告

#import "YLMessageBaseViewController.h"
#import "YLNewsTableViewCell.h"
#import "YLMessageCenterViewModel.h"
#import <Masonry.h>
#import "WHNoDataTableViewController.h"
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>
@interface YLMessageBaseViewController ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property (nonatomic, strong) YLMessageCenterViewModel *viewModel;
@property (nonatomic, strong) NSString *showString;
@end

@implementation YLMessageBaseViewController

- (YLMessageCenterViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLMessageCenterViewModel alloc] initWithIsLoop:self.messageType];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    __weak typeof(self) weakSelf = self;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.bottom.mas_equalTo(weakSelf.view);
    }];
    
        self.tableView.emptyDataSetSource = self;
        self.tableView.emptyDataSetDelegate = self;
    
    _showString = @"";

    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLNewsTableViewCell class]) bundle:nil] forCellReuseIdentifier:[YLNewsTableViewCell cellID]];
    [self.tableView reloadData];
    
    self.title = (self.messageType == YLMessageTypeNotice) ? @"通知" : @"公告";
    
    
    [[self.viewModel.requestCommand execute:nil] subscribeNext:^(id  _Nullable x) {
        weakSelf.tableView.emptyDataSetDelegate = weakSelf;
        weakSelf.tableView.emptyDataSetSource = weakSelf;
        [weakSelf.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString * text = @"暂时还没有收到消息!";
    
    NSDictionary * attributes = @ { NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0f ],
        NSForegroundColorAttributeName:YLColor(130, 130, 130)};
    
    return [[ NSAttributedString  alloc ] initWithString:text attributes:attributes];
}


- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return [UIImage imageNamed:@"nodata"];
}

- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView
{
    return 25.0f;
}


- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    return -64;
}




#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.models.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YLNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLNewsTableViewCell cellID]];
    
    cell.model = self.viewModel.models[indexPath.row];
    
    return cell;
}

@end
